package cn.blackbirds.blocks.model;

import java.util.ArrayList;
import java.util.List;

import cn.blackbirds.blocks.Globals;

/**
 * 
 * 地图模型
 * 
 * @author jang
 * 
 */
public class GameModel {

	private int cols;
	private int rows;

	private Block[][] blocks;

	public GameModel() {
		cols = Globals.MAP_COLS;
		rows = Globals.MAP_ROWS;
		blocks = new Block[cols][rows];
	}

	public void cleanup() {
		blocks = new Block[cols][rows];
	}

	public Block[][] getModel() {
		return blocks;
	}

	public boolean isFull() {
		for (int i = 0; i < cols; i++) {
			for (int j = 0; j < rows; j++) {
				if (getBlock(i, j) == null)
					return false;
			}
		}
		return true;
	}

	public void removeBlock(int x, int y) {
		setBlock(x, y, null);
	}

	public void setBlock(int x, int y, Block s) {
		blocks[x][y] = s;
	}

	public Block getBlock(int x, int y) {
		if (x < cols && y < rows)
			return blocks[x][y];
		else
			return null;
	}

	/**
	 * 从坐标轴中找出与指定毗邻的所有点
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public List<Block> findNearBy(int x, int y) {

		int x1;
		int y1;

		List<Block> results = new ArrayList<Block>();
		results.add(blocks[x][y]);

		/************** 加入Ｘ轴连线棋子 ****************/
		List<Block> xArray = new ArrayList<Block>();

		// 正向
		x1 = x;
		y1 = y;
		while (x1 + 1 < Globals.MAP_COLS
				&& blocks[x1 + 1][y1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1 + 1][y1].getName())) {
			x1++;
			xArray.add(blocks[x1][y1]);
		}
		// 负向
		x1 = x;
		y1 = y;
		while (x1 - 1 >= 0
				&& blocks[x1 - 1][y1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1 - 1][y1].getName())) {

			x1--;
			xArray.add(blocks[x1][y1]);
		}

		if (xArray.size() >= Globals.CONTINUOUS_BLOCKS - 1) {
			results.addAll(xArray);
		}

		/************** 加入Ｙ轴连线棋子 ****************/
		List<Block> yArray = new ArrayList<Block>();

		// 正向
		x1 = x;
		y1 = y;
		while (y1 + 1 < Globals.MAP_ROWS
				&& blocks[x1][y1 + 1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1][y1 + 1].getName())) {
			y1++;
			yArray.add(blocks[x1][y1]);
		}

		// 负向
		x1 = x;
		y1 = y;
		while (y1 - 1 >= 0
				&& blocks[x1][y1 - 1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1][y1 - 1].getName())) {
			y1--;
			yArray.add(blocks[x1][y1]);
		}
		if (yArray.size() >= Globals.CONTINUOUS_BLOCKS - 1) {
			results.addAll(yArray);
		}

		/************** 加入ＸＹ对角线连线棋子 ****************/
		//XY异号
		List<Block> xyDifArray = new ArrayList<Block>();

		x1 = x;
		y1 = y;
		while (x1 - 1 >= 0
				&& y1 + 1 < Globals.MAP_ROWS
				&& blocks[x1 - 1][y1 + 1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1 - 1][y1 + 1].getName())) {
			x1--;
			y1++;
			xyDifArray.add(blocks[x1][y1]);

		}
		

		x1 = x;
		y1 = y;
		while (x1 + 1 < Globals.MAP_COLS
				&& y1 - 1 >= 0
				&& blocks[x1 + 1][y1 - 1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1 + 1][y1 - 1].getName())) {
			x1++;
			y1--;
			xyDifArray.add(blocks[x1][y1]);

		}
		if (xyDifArray.size() >= Globals.CONTINUOUS_BLOCKS - 1) {
			results.addAll(xyDifArray);
		}

		//XY同号
		List<Block> xyArray = new ArrayList<Block>();
		
		x1 = x;
		y1 = y;
		while (x1 + 1 < Globals.MAP_COLS
				&& y1 + 1 < Globals.MAP_ROWS
				&& blocks[x1 + 1][y1 + 1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1 + 1][y1 + 1].getName())) {
			x1++;
			y1++;
			xyArray.add(blocks[x1][y1]);

		}

		x1 = x;
		y1 = y;
		while (x1 - 1 >= 0
				&& y1 - 1 >= 0
				&& blocks[x1 - 1][y1 - 1] != null
				&& blocks[x1][y1].getName().equalsIgnoreCase(
						blocks[x1 - 1][y1 - 1].getName())) {
			x1--;
			y1--;
			xyArray.add(blocks[x1][y1]);

		}

		if (xyArray.size() >= Globals.CONTINUOUS_BLOCKS - 1) {
			results.addAll(xyArray);
		}
		return results;
	}
}
